//基础请求抽象类

import 'package:changxindianzishangwu/model/FileInfos.dart';
import 'package:dio/dio.dart';

import 'http_urls.dart';

enum HttpMethod { GET, POST, DELETE }

abstract class EcBaseRequest {
  //路径参数
  var pathParams;
  //是否启用https
  var useHttps = false;
  //是否启用dio库
  var useDio = true;
  //参数列表
  Map<String, dynamic> params = Map();
  Map<String, dynamic> dioParams = Map();
  //header 列表
  Map<String, String> header = {};

  FormData formData = new FormData();

  //请求服务器地址
  String authority() {
    return HttpUrls.baseUrl();
  }

  //规定实现类的 接口请求方式 ⬆ 枚举定义
  HttpMethod httpMethod();

  //接口地址
  String path();
  //是否需要登录
  bool needLogin();
  //是否启用其他服务器地址
  bool beginElseServer();

  String beginElseServerUrl();
  //拼接整体地址
  dynamic url() {
    Uri uri;

    var pathStr = path();
    //拼接路径参数
    if (pathParams != null) {
      if (path().endsWith("/")) {
        pathStr = "${path()}$pathParams";
      } else {
        pathStr = "${path()}/$pathParams";
      }
    }
    //如果启用https
    if (useHttps) {
      if (beginElseServer()) {
        uri = Uri.https(beginElseServerUrl(), pathStr, params);
      } else {
        uri = Uri.https(authority(), pathStr, params);
      }
    } else {
      if (beginElseServer()) {
        uri = Uri.http(beginElseServerUrl(), pathStr, params);
      } else {
        uri = Uri.http(authority(), pathStr, params);
      }
    }

    print("base:ZdBaseRequest:url()::${uri}");
    //如果启用dio库
    if (useDio) {
      return uri.toString();
    }
    return uri;
  }

  ///添加参数
  EcBaseRequest add(String k, Object v) {
    params[k] = v.toString();
    return this;
  }

  ///添加header
  EcBaseRequest addHeader(String k, Object v) {
    header[k] = v.toString();
    return this;
  }

  ///添加参数
  EcBaseRequest addList(String k, List<String> r) {
    params[k] = r;
    return this;
  }

  ///添加参数
  EcBaseRequest addFileList(String k, List<FileInfos> r) {
    dioParams[k] = r;
    return this;
  }

  EcBaseRequest addData(FormData formDatas) {
    formData = formDatas;
    return this;
  }
}
